package com.hjm.project.agentInfo.agentActActive.service.impl;

import java.util.List;

import com.hjm.common.exception.BusinessException;
import com.hjm.common.utils.DateUtils;
import com.hjm.project.agentInfo.agentActActive.domain.AgentActActive;
import com.hjm.project.agentInfo.agentActActive.mapper.AgentActActiveMapper;
import com.hjm.project.agentInfo.agentActActive.service.IAgentActActiveService;
import com.hjm.project.agentInfo.agentActActive.utils.AgentPriceCommon;
import com.hjm.project.agentInfo.agentInfo.domain.AgentInfo;
import com.hjm.project.agentInfo.agentInfo.mapper.AgentInfoMapper;
import com.hjm.project.agentInfo.agentPolicy.domain.AgentPolicy;
import com.hjm.project.agentInfo.agentPolicy.mapper.AgentPolicyMapper;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import com.hjm.common.utils.text.Convert;

import javax.annotation.Resource;

/**
 * 代理政策Service业务层处理
 * 
 * @author maker-platform
 * @date 2024-03-21
 */
@Service
public class AgentActActiveServiceImpl implements IAgentActActiveService {
    @Resource
    private AgentActActiveMapper agentActActiveMapper;
    @Resource
    private AgentInfoMapper agentInfoMapper;
    @Resource
    private AgentPolicyMapper agentPolicyMapper;

    /**
     * 查询代理政策
     * 
     * @param id 代理政策ID
     * @return 代理政策
     */
    @Override
    public AgentActActive selectagentActActiveById(Integer id) {
        return agentActActiveMapper.selectagentActActiveById(id);
    }

    /**
     * 查询代理政策列表
     * 
     * @param agentActActive 代理政策
     * @return 代理政策
     */
    @Override
    public List<AgentActActive> selectagentActActiveList(AgentActActive agentActActive) {
        return agentActActiveMapper.selectagentActActiveList(agentActActive);
    }

    /**
     * 查询代理政策列表
     *
     * @param agentActActive 代理政策
     * @return 代理政策 一个结果
     */
    @Override
    public AgentActActive selectagentActActiveOne(AgentActActive agentActActive) {
        return agentActActiveMapper.selectagentActActiveOne(agentActActive);
    }

    /**
     * 新增代理政策
     * 
     * @param agentActActive 代理政策
     * @return 结果
     */
    @Override
    public int insertagentActActive(AgentActActive agentActActive) {
        agentActActive.setCreateTime(DateUtils.getNowDate());
        return agentActActiveMapper.insertagentActActive(agentActActive);
    }

    /**
     * 修改代理政策
     * 
     * @param agentActActive 代理政策
     * @return 结果
     */
    @Override
    public int updateagentActActive(AgentActActive agentActActive) {
        return agentActActiveMapper.updateagentActActive(agentActActive);
    }

    /**
     * 删除代理政策对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteagentActActiveByIds(String ids) {
        return agentActActiveMapper.deleteagentActActiveByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除代理政策信息
     * 
     * @param id 代理政策ID
     * @return 结果
     */
    @Override
    public int deleteagentActActiveById(Integer id) {
        return agentActActiveMapper.deleteagentActActiveById(id);
    }

    @Override
    public void vliAgentPrice(AgentPriceCommon agentPriceCommon) {
        AgentInfo agentInfo = agentInfoMapper.selectAgentInfoById(agentPriceCommon.getAgentNum());
        String belongAgent = String.valueOf(agentInfo.getBelongAgent());
        AgentPolicy ap = new AgentPolicy();
        ap.setAgentNum(belongAgent);
        ap.setActId(agentPriceCommon.getActId());
        if (!belongAgent.equals("100000000")) {
            List<AgentPolicy> belongPrices = agentPolicyMapper.selectAgentPolicyList(ap);
            if (CollectionUtils.isEmpty(belongPrices)) {
                throw new BusinessException("目标代理上级未配置政策【" + agentPriceCommon.getActId() + "】的结算");
            }
            AgentPriceCommon belongCommon = AgentPriceCommon.dbToView(belongPrices);
            AgentPriceCommon.valiAgentPrice(agentPriceCommon,belongCommon);
        }
        //目标代理直属下级最小底价
        ap.setAgentNum(agentInfo.getAgentNum());
        List<AgentPolicy> nextMinPolicyInfos = agentPolicyMapper.selectMinPrice(ap);
        if (CollectionUtils.isNotEmpty(nextMinPolicyInfos)) {
            AgentPriceCommon nextCommon = AgentPriceCommon.dbToView(nextMinPolicyInfos);
            AgentPriceCommon.valiNextPrice(agentPriceCommon,nextCommon);
        }

    }
}
